% This function computes the following equilibrium objects:

% - Domestic spending associated with the labor market clearing condition (SP)
% - Domestic spending associated with consumer budget constraint (SP2)
% - Price index of the locally-produced manufacturing bundle (P)
% - Aggregate exports measured in domestic labor (RX)
% - Aggregate imports of inputs measured in domestic labor (M)
% - Consumer income measured in domestic labor (Iw)
% - Expenditure share in the non-traded good (sN)
% - Expenditure share in the imported final good (sM)
% - Overall consumer price index (Ptot)
% - Labor used in the non-tradable sector (LN)

% Notes:
% (1) Equilibrium ocurrs whenever SP=SP2
% (2) T denotes transfers measured in domestic labor (i.e., T/w)

% See Appendix for details


function [SP,SP2,P,RX,M,Iw, sN, sM,Ptot,LN] = GEobjects(w,sD, sX, phi,fc,fx, param)

g = param.gamma;
eps = param.eps;
sg = param.sg;
eta = param.eta;
beta = param.beta;
qD = param.qD;
theta = param.theta;
vartheta = param.vartheta;
aN = param.aN;
AN = param.AN;
zeta = param.zeta;
varrho = param.varrho;
pM = param.pM;
T = param.T;
FXM = param.FXM; FX = param.FX; FM = param.FM;

pMw = pM / w;

% Constants:

k1 =  qD^(g*(sg-1)) * (1-g)^((1-g)*(sg-1)) * g^(g*(sg-1)) * g * (sg-1)^sg/sg^sg * eta; % kappa2_wiggle
k2 = qD^(g*(sg-1)) * (sg-1)^(sg-1)/sg^sg * (1-g)^((1-g)*(sg-1))* g^(g*(sg-1)); % kappa3
k3 = k2 * (theta-1)/theta; % kappa3*times (theta-1)/theta
k5 = (sg / (sg-1))^(1-sg) * (1-g)^((1-g)*(sg-1)) * (g*beta^(eps/(eps-1))*qD)^(g*(sg-1)); % kappa1
k6 = mean(k1*(sD.^(-1)-1).^(1/(eps-1)/eta) .* fc + k2 .* ((sD<1).*(sX==0)) * FM + k3 * (sX./(1-sX)).^(theta/(theta-1)) .*fx + k2 * ((sX>0).*(sD==1)) * FX + k2 * ((sX>0).*(sD<1)) * FXM ); % \varPsi
k7 = mean(phi.^(sg-1) .* (sX./(1-sX)) .*sD.^(-g*(sg-1)/(eps-1))); % \Upsilon
k8 = mean(phi.^(sg-1) .* (1./(1-sX)) .*sD.^(1-g*(sg-1)/(eps-1))); % \Gamma

% Price indeces and expenditure shares:
P = (sg/ (sg-1))^(1/(1-g)) * (1-g)^(-1) * (g*beta^(eps/(eps-1))*qD)^(-g/(1-g)) * (mean(phi.^(sg-1).*sD.^(-g*(sg-1)/(eps-1))))^(1/((1-sg)*(1-g))); % price index of locally-produced manufacturing bundle measured in domestic labor (P/w)
PM = (varrho^zeta * pMw^(1-zeta) + (1-varrho)^zeta * P^(1-zeta)).^(1/(1-zeta)); % price index of manufacturing bundle measured in domestic labor (PM/w) 
pNw = 1/AN; % price of non-tradable good measured in domestic labor (pN/w)
xx = PM/pNw; % relative price of manufacturing bundle to non-tradable good
sN = aN^vartheta ./ (aN^vartheta + (1-aN)^vartheta *xx.^(1-vartheta)); % expenditure share on non-tradable
sM = varrho^zeta ./ (varrho^zeta + (1-varrho)^zeta *(P/pMw).^(1-zeta))  ; % expenditure share on imported final good
aT = (1-sN)* (1- sM); % share of expenditure on locally-produced manufacturing bundle (alpha wiggle in paper's notation)

% Obtain S/w associated with labor market clearing (see Calibration Appendix):
Ld_norm = (sg-1)/sg * (1-g) * (  k5 * k7 * P^((sg-1)*(1-g)) + 1) + P^((sg-1)*(1-g)) * k6 + sN/aT * ( 1 - (sg-1)/sg * g *k5 *k8 * P^((sg-1)*(1-g)) );
Sw = param.L / Ld_norm; 
% Obtain S/w associated with consumer budget constraint (see Calibration Appendix):
Sw2 = (1/aT - 1/ aT * g* (sg-1)/sg * k5* P^((sg-1)*(1-g)) * k8 + k6*P^((sg-1)*(1-g)) - 1/sg - k5/sg * P^((sg-1)*(1-g))*k7 ).^(-1) * (param.T + param.L) ;

LF  = k6 * Sw * P^((sg-1)*(1-g)); % labor used for fixed costs
SX = g* (sg-1)/sg * k5* P^((sg-1)*(1-g)) * k8 * Sw; % expenditure in local inputs by local firms measured in domestic labor (SX/w)
RX = k5  * P^((sg-1)*(1-g)) * Sw *k7; % aggregate exports measured in domestic labor (RX/w)
M = g * (sg-1)/sg * (Sw + RX) - SX; % imports of inputs measured in domestic labor (M/w)
Iw = param.L + T + Sw/sg + RX/sg - LF; % consumer income measured in domestic labor (I/w)
Ptot = (aN^vartheta .* pNw.^(1-vartheta) + (1-aN)^vartheta *PM.^(1-vartheta)).^(1/(1-vartheta)); % overall consumer price index (CES price index of the non-tradable price and the price index of the manufacturing bundle) measured in domestic labor
LN =  sN/aT * ( 1 - (sg-1)/sg * g *k5 *k8 * P^((sg-1)*(1-g)) )*Sw; % labor in non-tradable
P = P*w; SP = Sw*w; SP2 = Sw2*w; Ptot = Ptot *w;
end